<template>
  <div class="page-container page-footer-btn">
      <van-row>
        <van-col span="24">
          <FortenScan
            ref="scanDom"
            propPlaceholder="请扫描标签拣货/取消拣货"
            propErrorMsg="请扫描正确的标签"
            :propTypeArr="['sn','rfid']"
            @scanCall="handleScanOver"
            :disabled="strings.isBlank(transfer.id)"
          />
        </van-col>
      </van-row>
      <van-row>
        <van-cell>
          <template #title>
            <div>
              <van-row>
                <van-col span="24" style="padding-left: 0px">
                  <van-field v-if="strings.isBlank(transfer.id)" :disabled="strings.isNotBlank(transfer.id)" is-link @click="showPopup" placeholder="请选择仓库:" label="调入仓库" :value="transfer.toWarehouseName"></van-field>
                  <span v-else>调入仓库:{{transfer.toWarehouseName}}</span>
                </van-col>
              </van-row>
              <van-row>
                <van-col span="16">调拨单号:{{transfer.busKey}}</van-col>
                <van-col span="8">调拨金额:{{transfer.amount}}</van-col>
              </van-row>
              <van-row>
                <van-col span="16">拣货时间:{{transfer.pickDate}}</van-col>
                <van-col span="8">拣货人:{{transfer.picker}}</van-col>
              </van-row>
              <van-row>
                <van-col span ="16" style="display:flex;">结算类型：
                  <van-radio-group :disabled="strings.isNotBlank(transfer.id)" v-model="transfer.settlementType" direction="horizontal" v-for="dict in utils.getDictDataList('settlement_type')" :key="dict.value">
                    <van-radio :name="dict.value">{{dict.label}}</van-radio>
                  </van-radio-group>
                </van-col> 
                <van-col span="8">调出仓库:{{transfer.fromWarehouseName}}</van-col>                 
              </van-row>
            </div>
          </template>
        </van-cell>
      </van-row>
    <van-divider
      :style="{ color: '#1989fa', borderColor: '#1989fa' }"
    >
      拣货明细
    </van-divider>
    <van-row v-for="item in listItems"
             :key="item.id"
             :name="item.id">
      <van-cell>
        <stock-item :item="item"></stock-item>
      </van-cell>
    </van-row>
      <van-row type="flex" justify="space-around" class="footer-btn">
        <van-col span="5" v-if="[undefined, 'start'].includes(transfer.flowStatus)">
          <van-button type="primary" @click="handleBegin" block :disabled="['picking', 'picked'].includes(transfer.flowStatus)">
            开始拣货
          </van-button>
        </van-col>
        <van-col span="5" v-if="[undefined, 'start','picking', 'picked'].includes(transfer.flowStatus)">
          <van-button type="primary" @click="handleEndPick" block :disabled="transfer.flowStatus != 'picking'">
            拣货完成
          </van-button>
        </van-col>
        <van-col span="5" v-if="[undefined, 'start','picking', 'picked'].includes(transfer.flowStatus)">
          <van-button type="danger" @click="handleCancelPick" block :disabled="transfer.flowStatus != 'picked'">
            拣货撤销
          </van-button>
        </van-col>
        <van-col span="5">
          <van-button type="info" block @click="utilPrint.print('transfer',transfer.id )" :disabled="strings.isBlank(transfer.id)">
            打印标签
          </van-button>
        </van-col>
        <van-col span="5" v-if="![undefined, 'start'].includes(transfer.flowStatus)">
          <van-button type="warning" @click="handleDist" block :disabled="transfer.flowStatus != 'picked'">
            开始配送
          </van-button>
        </van-col>
      </van-row>

    <!-- 选择科室-->
    <FortenList ref="listItem" :propAutoFetchData = 'false'
                :propIsRadio="true"
                :propFlagPopup="true"
                :propSearchFields="['name']"
                :propSearchTextAppend="'调入仓库'"
                :propPreQueryWrapper="$newWrapper()"
                propUrl="/base/warehouse/page"
                @change="warehouseCheck"
    >
      <template  #default="{item}"  >
        <van-row >
          <van-col span="4">
            <van-radio :name="item"  />
          </van-col>
          <van-col span="20">{{item.name}}</van-col>
        </van-row>
      </template>
    </FortenList>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import barcodeParse from '@/util/barcodeParse.js'

export default {
  name: 'detail',
  data() {
    return {
      scan: '',
      transfer: {},
      listItems: [],
      snQtyPicked: 0
    }
  },
  mounted() {
    this.showPopup()
    this.getBaseData()
  },
  methods: {
    getBaseData() {
      this.$set(this.transfer, 'fromWarehouseId', this.utils.getCurrentWarehouse().warehouseId)
      this.$set(this.transfer, 'fromWarehouseName', this.utils.getCurrentWarehouse().warehouseName)
    },   
    //扫描
    handleScanOver(scanValue) {
      this.$ajax.post('/wm/transfer_item/scan', {
        isScan: 1,
        autoUnlock: true,
        transferId: this.transfer.id,
        uniqueCode: scanValue,
        fromWarehouseId: this.transfer.fromWarehouseId
      }).then(resp => {
        if (resp.code == 0) {
          this.$notify.success(resp.msg)
          this.getData()
          this.scan = ''
          this.$refs.scanDom.focus()
        }
      })
    },
    // 加载调拨明细
    getData() {
      let wrapper = this.$newWrapper()
        .eq('transferId', this.transfer.id)
      const qString = JSON.stringify(wrapper.queryConditions)
      const params = {
        queryString: qString,
      }
      this.$ajax.get('/wm/transfer_item/list', {params}).then(resp => {
        if (resp.code == 0) {
          this.listItems = resp.data
          this.transferPick.labelQty = resp.data.length
        }
      })
    },
    //开始拣货-创建调拨单
    handleBegin() {
      this.$set(this.transfer, 'fromWarehouseId', this.utils.getCurrentWarehouse().warehouseId)
      this.$set(this.transfer, 'fromWarehouseName', this.utils.getCurrentWarehouse().warehouseName)      
      if (this.transfer.fromWarehouseId == this.transfer.toWarehouseId) {
        this.$notify.warning('调入仓库跟调出仓库不能一样，请重新选择')
        return
      }
      if (this.strings.isBlank(this.transfer.fromWarehouseId)) {
        this.$notify.warning('请先到工作台选好工位再进行操作')
        return
      }
      if (this.strings.isBlank(this.transfer.toWarehouseId)) {
        this.$notify.warning('请先选择调入仓库')
        this.showPopup()
        return
      }
      if (this.strings.isBlank(this.transfer.settlementType)) {
        this.$notify.warning('请先选择结算类型')
        return
      }
      this.utils.loadShow()
      this.$set(this.transfer, 'flowStatus', 'picking')
      this.$set(this.transfer, 'busDate', new Date().format(this.dates.defaultDatePattern))
      this.$set(this.transfer, 'busUser', this.utils.getCurrentUser().user.name)
      this.$set(this.transfer, 'pickDate', new Date().format(this.dates.defaultDateTimePattern))
      this.$set(this.transfer, 'picker', this.utils.getCurrentUser().user.name)
      this.$set(this.transfer, 'transferType', '')
      this.$ajax.post('/wm/transfer/save', this.transfer).then(resp => {
        if (resp.code == 0) {
          this.$notify.success('单据创建成功，请开始拣货')
          this.transfer = resp.data
          setTimeout(() => {
            this.$refs.scanDom.focus()
          }, 500)
        }
        this.utils.loadHide()
      })
    },
    //拣货完成
    handleEndPick() {
      this.utils.loadShow()
      let param = {
        busId: this.transfer.id,
        buttonKey: 'end_pick',
        flowKey: 'wm_moving',
        flowStatusTextFrom: '拣货中',
        flowStatusTextTo: '拣货完成',
        busTable: 'wm_moving',
        busIdName: this.transfer.id,
        busTypeName: 'transfer/transfer-pick-fast'
      }
      this.$ajax.post('/wm/transfer/flow_change', param).then(resp => {
        if (resp.success) {
          this.$notify.success(resp.msg)
          this.transfer.flowStatus = 'picked'
        }
        this.utils.loadHide()
      })
    },
    showPopup() {
      this.$refs.listItem.show()
    },
    handleCancelPick() {
      this.$dialog.confirm({
        title: '提示',
        message: '是否撤销拣货',
      })
        .then(() => {
          this.$ajax.post(`/wm/transfer_item/cancelTransferPick/${this.transfer.id}`, {})
            .then(resp => {
              if (resp.code == 0) {
                this.$notify.success(resp.msg)
                this.transfer.flowStatus = 'start'
                this.getData()
              }
            })
        })
        .catch(() => {
        })
    },
    // 开始配送
    handleDist() {
      this.utils.loadShow()
      let param = {
        busId: this.transfer.id,
        buttonKey: 'dist',
        flowKey: 'wm_moving',
        flowStatusTextFrom: '拣货完成',
        flowStatusTextTo: '开始配送',
        busTable: 'wm_moving',
        busIdName: this.transfer.id,
        busTypeName: 'transfer/transfer-pick-fast'
      }
      this.$ajax.post('/wm/transfer/flow_change', param).then(resp => {
        if (resp.success) {
          this.$notify.success(resp.msg)
          this.transfer.flowStatus = 'distributing'
        }
        this.utils.loadHide()
      })
    },
    warehouseCheck(item) {
      this.$set(this.transfer, 'toWarehouseId', item.id)
      this.$set(this.transfer, 'toWarehouseName', item.name)
    }
  },
  computed: {}
}
</script>

<style lang="scss" scoped>

</style>
